package com.guguskins.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guguskins.back.entity.GuBox;
import com.guguskins.back.entity.GuDiscount;
import com.guguskins.back.entity.GuGoods;
import com.guguskins.back.entity.GuProp;
import com.guguskins.back.entity.vo.MarketTypeVo;
import com.guguskins.back.entity.vo.PageVo;
import com.guguskins.back.entity.vo.ResultVo;
import com.guguskins.back.service.GuBoxService;
import com.guguskins.back.service.GuDiscountService;
import com.guguskins.back.service.GuGoodsService;
import com.guguskins.back.service.GuPropService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/prize")
public class GuPrizeController {
    @Autowired
    private GuBoxService boxService;
    @Autowired
    private GuPropService propService;
    @Autowired
    private GuGoodsService goodsService;
    @Autowired
    private GuDiscountService discountService;
    @GetMapping("back/findByItemType")
    public ResultVo backFindByIteamType(
            @RequestParam(value = "itemType",required = false)Integer itemType,
            @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize,
            @RequestParam(value = "name",required = false,defaultValue = "")String name
    ){
       List<MarketTypeVo> marketTypeVos=new ArrayList<>();
       if(itemType==1){
           IPage<GuBox> page = new Page<>(pageNum, pageSize);
           LambdaQueryWrapper<GuBox> queryWrapper=new LambdaQueryWrapper();
           queryWrapper.in(GuBox::getBoxType,1,0);
           queryWrapper.like(!ObjectUtils.isEmpty(name),GuBox::getBoxName,name);
           page=boxService.page(page,queryWrapper);
           List<GuBox> boxList = page.getRecords();

           boxList.forEach(guBox -> {
               MarketTypeVo marketTypeVo=new MarketTypeVo();
               marketTypeVo.setItemId(guBox.getBoxId());
               marketTypeVo.setItemName(guBox.getBoxName());
               marketTypeVos.add(marketTypeVo);
           });
           return ResultVo.success(new PageVo<>(marketTypeVos,page.getTotal()),"盒子");
       }
       if(itemType==3){
           IPage<GuProp> page=new Page<>(pageNum,pageSize);
           LambdaQueryWrapper<GuProp> queryWrapper=new LambdaQueryWrapper<>();
           queryWrapper.like(!ObjectUtils.isEmpty(name),GuProp::getPropName,name);
           page=propService.page(page,queryWrapper);
           List<GuProp> props = page.getRecords();
           props.forEach(guProp -> {
               MarketTypeVo marketTypeVo=new MarketTypeVo();
               marketTypeVo.setItemId(Long.parseLong(guProp.getPropId().toString()));
               marketTypeVo.setItemName(guProp.getPropName());
               marketTypeVos.add(marketTypeVo);
           });
           return ResultVo.success(new PageVo<>(marketTypeVos,page.getTotal()),"道具");
       }
       if(itemType==2){
           IPage<GuGoods> page=new Page<>(pageNum,pageSize);
           LambdaQueryWrapper<GuGoods> queryWrapper=new LambdaQueryWrapper<>();
           queryWrapper.like(!ObjectUtils.isEmpty(name),GuGoods::getGoodsName,name);
          page=goodsService.page(page,queryWrapper);
           List<GuGoods> goods = page.getRecords();
           goods.forEach(guGoods -> {
               MarketTypeVo marketTypeVo=new MarketTypeVo();
               marketTypeVo.setItemId(guGoods.getGoodsId());
               marketTypeVo.setItemName(guGoods.getGoodsName());
               marketTypeVos.add(marketTypeVo);
           });
           return ResultVo.success(new PageVo<>(marketTypeVos,page.getTotal()),"饰品");
       }
       if(itemType==4){
           IPage<GuDiscount> page=new Page<>(pageNum,pageSize);
           LambdaQueryWrapper<GuDiscount> queryWrapper=new LambdaQueryWrapper<>();
           queryWrapper.like(!ObjectUtils.isEmpty(name),GuDiscount::getDcTitle,name);
           page=discountService.page(page,queryWrapper);
           List<GuDiscount> discounts = page.getRecords();
           discounts.forEach(discount -> {
               MarketTypeVo marketTypeVo=new MarketTypeVo();
               marketTypeVo.setItemId(Long.parseLong( discount.getDcId().toString()));
               marketTypeVo.setItemName(discount.getDcTitle());
               marketTypeVos.add(marketTypeVo);
           });
           return ResultVo.success(new PageVo<>(marketTypeVos,page.getTotal()),"奖券");
       }
        if(itemType==5){
            IPage<GuBox> page=new Page<>(pageNum,pageSize);
            LambdaQueryWrapper<GuBox> queryWrapper=new LambdaQueryWrapper();
            queryWrapper.eq(GuBox::getBoxType,2);
            queryWrapper.like(!ObjectUtils.isEmpty(name),GuBox::getBoxName,name);
           page=boxService.page(page,queryWrapper);
            List<GuBox> boxList = page.getRecords();
            boxList.forEach(guBox -> {
                MarketTypeVo marketTypeVo=new MarketTypeVo();
                marketTypeVo.setItemId(guBox.getBoxId());
                marketTypeVo.setItemName(guBox.getBoxName());
                marketTypeVos.add(marketTypeVo);
            });
            return ResultVo.success(marketTypeVos,"盒子");
        }
       return null;
    }

}
